/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pbv.azsg.dao;

import java.util.List;
import java.util.Date;

import com.pbv.azsg.domain.Dispenser;
import com.pbv.azsg.domain.DispenserH;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


/**
 *
 * @author pbv
 */
@Repository
public class DispenserDAOImpl implements IDispenserDAO {
    
    @Autowired
    protected SessionFactory sessionFactory;
    
    @Override
    public List<Dispenser> getAllDispensers(){
        return (List<Dispenser>) 
          this.sessionFactory
              .getCurrentSession()
              .createCriteria(Dispenser.class)
              .list();
    }

    
    @Override
    public Dispenser getDispenser(Long id) {
        return (Dispenser) 
          this.sessionFactory
              .getCurrentSession()
              .createCriteria(Dispenser.class)
              .add(Restrictions.eq("id", id))
              .list()
              .get(0);
    }

    @Override
    public Dispenser updateDispenser(Dispenser dispenser){
        dispenser.setModified( new Date() );
        DispenserH dispenserH = new DispenserH( dispenser, "ADD");
        this.sessionFactory.getCurrentSession().saveOrUpdate(dispenser);        
        this.sessionFactory.getCurrentSession().save(dispenserH);
        return dispenser;
    }
    
}
